package wds

import (
	"fmt"
	"pool"
	"net/http"
	"encoding/json"
	"labix.org/v2/mgo/bson"
)

func Userwds(w http.ResponseWriter, r *http.Request) {
	// UID
	cookie, err := r.Cookie("uid")
	if err != nil {
		return;
	}
	uid := cookie.Value
	// fmt.Println("Userwds", uid)

	resD := bson.M{"ret": 0}

	// 查询当前用户wd
	qbson := bson.D{ {"distinct", "words"}, {"key", "wd"}, {"query", bson.D{ {"uid", uid} }} }
	rbson := bson.D{}

	fmt.Println("len_mg:", len(pool.MongoDB))
	db := <-pool.MongoDB
	err = db.Run(qbson, &rbson)
	if err != nil {
		// db.Run 错误
		resD["ret"] = 2001;
	} else {
		resD["wds"] = rbson.Map()["values"]
	}
	pool.MongoDB <- db


	resB, _ := json.Marshal(resD)
	res := string(resB)
	fmt.Fprintf(w, "%s", res)
}